/* Copyright (c) 2010, Peter Lényi */
/* Licensed under The BSD License. */

package gem.apps.fs.web;

import gem.apps.fs.engine.FileServer;
import gem.apps.fs.entities.FileMeta;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet class FileUpdatePostServlet checks password and updates file metadata.
 *
 * @author Peter Lényi
 * @version 20100509F2
 */
public class FileUpdatePostServlet extends HttpServlet
{
    private static final String ADDRESS_FATAL = "/error/fatal/jsp";
    private static final String ADDRESS_WARNING = "/file/update/jsp";
    private static final String ADDRESS_SUCCESS = "/file/details/pre";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // prepare local variables
        boolean success = false;
        boolean validationError = false;
        List<String> messages = new ArrayList<String>();

        // prepare resources
        FileServer fs = FileServer.getInstance();
        ResourceBundle gui = ResourceBundle.getBundle("Gui");

        // load request parameters
        String uinString = request.getParameter("form.uin");
        String title = request.getParameter("form.title");
        String comment = request.getParameter("form.comment");
        String password = request.getParameter("form.password");
        String mime = request.getParameter("form.mime");
        String oldPassword = request.getParameter("form.password.old");
        if (oldPassword == null) {
            oldPassword = "";
        }

        // validate data
        try {
            long uin = Long.parseLong(uinString);
            if (title == null || title.equals("")) {
                messages.add(gui.getString("Gui.file.form.title.err"));
                validationError = true;
            }
            if (comment == null) {
                comment = "";
            }
            if (password == null) {
                password = "";
            }
            if (mime == null || mime.equals("")) {
                messages.add(gui.getString("Gui.file.form.mime.err"));
                validationError = true;
            }
            
            // authorization
            FileMeta filemeta = fs.getFileMeta(uin);
            if (!filemeta.getPassword().equals(oldPassword)) {
                messages.add(gui.getString("Gui.action.authorize.no"));
                validationError = true;
            }

            // execute logic
            if (!validationError) {
                // update file metadata
                if (fs.updateFile(uin, title, comment, password, mime)) {
                    messages.add(gui.getString("Gui.action.file.update.done"));
                    success = true;
                }
                else {
                    messages.add(gui.getString("Gui.action.file.update.err"));
                }
            }

            // pass connection
            request.getSession().setAttribute("msg", messages);
            if (success && !validationError) {
                response.sendRedirect(getServletContext().getContextPath() + ADDRESS_SUCCESS + "?uin=" + uin);
            }
            else {
                response.sendRedirect(getServletContext().getContextPath() + ADDRESS_WARNING + "?uin=" + uin);
            }
        }
        catch (NumberFormatException ex) {
            response.sendRedirect(getServletContext().getContextPath() + ADDRESS_FATAL);
        }        
    }
}
